package com.mhwx.mhlive.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.mhwx.mhlive.MyApplication;
import com.mhwx.mhlive.R;
import com.mhwx.mhlive.ui.base.BaseActivity;
import com.mhwx.mhlive.ui.fragment.ChoiceFragment;
import com.mhwx.mhlive.ui.fragment.MeFragment;
import com.mhwx.mhlive.ui.widget.FinishProjectPopupWindows;
import com.mhwx.mhlive.ui.widget.NestRadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements   NestRadioGroup.OnCheckedChangeListener,View.OnClickListener{
    
    
    private NestRadioGroup mRadioGroup;
    private RadioButton mHost, mVip;
    public Fragment choiceFragment,MeFragment,fromFragment;
    @BindView(R.id.btn_sign_in)
    ImageView btn_sign_in;
    private FinishProjectPopupWindows mFinishProjectPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }
   private void init(){


       mRadioGroup = (NestRadioGroup) findViewById(R.id.home_radio_group);
       mRadioGroup.setOnCheckedChangeListener(this);

       mHost = (RadioButton) findViewById(R.id.host_fragment);
       mHost.setChecked(true);
       mVip = (RadioButton) findViewById(R.id.vip_fragment);

       btn_sign_in.setOnClickListener(this);

       mFinishProjectPopupWindow = new FinishProjectPopupWindows(MainActivity.this, null);
   }




    private void commite(Fragment from) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.realtabcontent, from).commit();
    }

    @Override
        public void onCheckedChanged(NestRadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.host_fragment:
                if (choiceFragment == null) {
                    choiceFragment = new ChoiceFragment();
                    commite(choiceFragment);
                } else {
                    commitFragment(fromFragment, choiceFragment);
                }
                fromFragment = choiceFragment;

                break;
            case R.id.vip_fragment:
                MyApplication.mediaRef = "vip";
                if (MeFragment == null)
                    MeFragment = new MeFragment();
                commitFragment(fromFragment, MeFragment);
                fromFragment = MeFragment;

                break;

        }


    }
    public void commitFragment(Fragment from, Fragment to) {
        if (from == to) {
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        if (from == null) {
            fragmentTransaction.show(to).commit();
        } else {
            if (!to.isAdded()) { // 先判断是否被add过
                fragmentTransaction.hide(from).add(R.id.realtabcontent, to)
                        .commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                fragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_sign_in:

                mFinishProjectPopupWindow.showAtLocation(findViewById(R.id.activity_main),
                        Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
        }
    }
}
